package com.emenu;

import java.util.ArrayList;
import java.util.List;

import com.emenu.RightTopFragment.RightAdapter;
import com.emenu.application.StaticObject;
import com.emenu.bean.FunBean;
import com.emenu.bean.Menu;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class FunRightFragment extends ListFragment {
	List<FunBean> list = new ArrayList<FunBean>();
	FragmentManager fm;
	StaticObject SO;
	Context cn;

	FunTimeFragment lf;

	private FunMemberListener funMemberListener;

	public void setFunMemberListener(FunMemberListener funMemberListener) {
		this.funMemberListener = funMemberListener;
	}

	private List<FunBean> getDate() {
		return list;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		getDate();
		setListAdapter(new FunRightAdapter());
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);

		SO = (StaticObject) getActivity().getApplication();
		cn = this.getActivity();
		fm = ((FunMainActivity) cn).getSupportFragmentManager();

		lf = (FunTimeFragment) fm.findFragmentById(R.id.funleft);
		list = SO.getFunBeanList();
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);

		if (funMemberListener != null) {
			funMemberListener.onFunMemberSelected(list.get(position));

			FragmentTransaction ft = fm.beginTransaction();
			int h = list.get(position).getId();
			if (h == 1) {

				FunTimeFragment FunTime = new FunTimeFragment();
				ft.replace(R.id.funleft, FunTime);
			} else if (h == 2) {

				FunhitFragment funhitFragment = new FunhitFragment();
				ft.replace(R.id.funleft, funhitFragment);
			}else if (h == 3) {

				FunRetailPrint funRetailPrint = new FunRetailPrint();
				ft.replace(R.id.funleft, funRetailPrint);
			} else if (h == 4) {

				FunCheckStatusFragment funCheckStatusFragment = new FunCheckStatusFragment();
				ft.replace(R.id.funleft, funCheckStatusFragment);
			}else if (h == 5) {

				FunMenuHitFragment funMenuHitFragment = new FunMenuHitFragment();
				ft.replace(R.id.funleft, funMenuHitFragment);
			}else if (h == 6) {

				FunChgPW funChgPW = new FunChgPW();
				ft.replace(R.id.funleft, funChgPW);

			}

			ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
			ft.addToBackStack(null);

			ft.commit();
		}
	}

	public class FunRightAdapter extends BaseAdapter {
		@Override
		public int getCount() {

			return list.size();
		}

		@Override
		public Object getItem(int position) {

			return list.get(position);
		}

		@Override
		public long getItemId(int position) {

			return position;

		}

		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = ((FunMainActivity) cn).getLayoutInflater()
						.inflate(R.layout.funright, null);
			}
			TextView textViewname;

			textViewname = (TextView) convertView
					.findViewById(R.id.tv_funrightname);

			if (position % 2 == 1) {
				textViewname.setBackgroundColor(R.color.right2);
			}
			textViewname.setText((String) list.get(position).getName());

			return convertView;
		}

	}

}
